package com.hbzjt2012.lesson04;

import javax.net.ssl.HttpsURLConnection;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLDownload {
    public static void main(String[] args) throws Exception {
        // 1.下载地址
        URL url = new URL("https://view.lixingyong.com/images/2021/12/08/u30255374911751147171fm26fmtauto.jpg");
        // 2.连接到这个资源 HTTPS
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();

        FileOutputStream fos = new FileOutputStream("a.jpg");

        byte[] buffer = new byte[1024];
        int len;

        while ((len = inputStream.read(buffer)) != -1){
            fos.write(buffer,0,len);    // 写出这个数据
        }

        fos.close();
        inputStream.close();
        urlConnection.disconnect(); // 断开链接

    }
}
